package com.edu.oracle.controller;

import com.edu.oracle.pojo.Course;
import com.edu.oracle.pojo.Teacher;
import com.edu.oracle.service.CourseService;
import com.edu.oracle.service.TeacherService;
import com.edu.oracle.service.impl.CourseServiceImpl;
import com.edu.oracle.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class CourseController extends HttpServlet {//这里的controller是servlet

    //引入service业务层  new一个业务层对象的类
    CourseService courseService = new CourseServiceImpl();
    TeacherService teacherService = new TeacherServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse respont) throws ServletException, IOException {
        //获取请求

        //1.获取请求路径
        String requestURI =request.getRequestURI();
        System.out.println("截取前的url:"+requestURI);

        String substring = requestURI.substring(31);//   /studentWebSystem_war_exploded/
        //String substring1 = requestURI.substring(Integer.parseInt("31"));//   /studentWebSystem_war_exploded/
        System.out.println(substring);

        //2、判断请求处理
        if(substring.equals("listCourse")){//处理查询所有的course表信息
            //1.获取信息

            //2.调用方法
            List<Course> courseList = courseService.list();

            //3.返回结果
            request.setAttribute("courseList",courseList);//内置对象存储信息携带到前端
            request.getRequestDispatcher("listCourse.jsp").forward(request,respont);//转发到页面
        }else if(substring.equals("deleteCourse")){
            //1.获取数据
            String cno = request.getParameter("cno");

            //2.调用service业务层删除方法
            boolean bl = courseService.delete(cno);

            //3.处理结果
            //请求servlet
            request.getRequestDispatcher("listCourse").forward(request,respont);
        }

        //实现添加页面转发
        //判断是否请求添加页面转发处理
        else if(substring.equals("addCourseForward")){
            //处理转发页面
            //TODO  查询教师表教师ID和姓名， 给前端返回教师姓名，ID
            request.getRequestDispatcher("addCourse.jsp").forward(request,respont);
        }
        else if(substring.equals("addCourse")){//实现添加功能
            //1.接收信息
            request.setCharacterEncoding("utf-8");
            Course course = new Course();

            course.setCno(request.getParameter("cno"));
            course.setCname(request.getParameter("cname"));
            course.setCredit(Integer.parseInt(request.getParameter("credit")));//字符串转换成数字类型
            //course.setTno(request.getParameter("tno"));

            System.out.println(course.getCno()+course.getCname()+course.getCredit());

            //2.调用业务层  调用添加方法
            Boolean bl = courseService.add(course);

            //3.返回结果
            if (bl){//添加成功，请求查询所有servlet路径，实现查询所用和转发到查询所用列表
                request.getRequestDispatcher("listCourse").forward(request,respont);
            }else {//添加失败，转发到添加页面，继续添加
                request.getRequestDispatcher("addCourse.jsp").forward(request,respont);
            }
        }

        //更新页面转发
        else if(substring.equals("viewCourse")){
            //1.获取数据
            String cno = request.getParameter("cno");
            System.out.println(cno+":更新请求第一步  查询一个人信息");

            //2.调用业务层的查询方法
            Course course = courseService.selectByOne(cno);

            List<Teacher> teacherList =teacherService.list();
            course.setTeacherList(teacherList);

            for (int i = 0; i < course.getTeacherList().size(); i++) {
                System.out.println(course.getCname());
            }

            //3.处理结果
            if (course!=null){//查询成功，转发到更新页面
                request.setAttribute("course",course);
                request.getRequestDispatcher("updateCourse.jsp").forward(request,respont);
            }else{//失败，请求查询所有servlet路径，实现查询所用和转发到查询所用列表
                request.getRequestDispatcher("listCourse").forward(request,respont);
            }
        }

        //更新实现数据功能
        else if(substring.equals("updateCourse")){
            //1.获取数据
            request.setCharacterEncoding("utf-8");

            //1.1  mvc中实现model存储数据   model就是实体类
            Course course = new Course();
            course.setCno(request.getParameter("cno"));
            course.setCname(request.getParameter("cname"));
            course.setCredit(Integer.parseInt(request.getParameter("credit")));
            course.setTno(request.getParameter("tno"));

            System.out.println(course.getTno()+"===============");

            //2.调用业务层的更新方法
            boolean bl =courseService.update(course);

            //3.处理结果
            //暂时不判断
            request.getRequestDispatcher("listCourse").forward(request,respont);
        }
    }
}
